package com.bike.service.impl;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.HhAgent;
import com.bike.entity.HhBankBranch;
import com.bike.entity.HhCity;
import com.bike.mapper.HhAgentMapper;
import com.bike.mapper.HhBankBranchMapper;
import com.bike.mapper.HhCityMapper;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.bike.util.TransFerUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class HhAgentServiceImpl extends BaseServcieImpl  {

	@Autowired
	HhAgentMapper hhAgentMapper;
	@Autowired
	HhCityMapper cityMapper;
	@Autowired
	HhBankBranchMapper bankBranchMapper;
	
	/**
	* 保存
	*/
	public void saveOrUpdate(PageData pd, HhAgent record) {
		
		Long cityparentid=pd.getLong("cityparentid");
		HhCity city=cityMapper.selectByPrimaryKey(cityparentid);
		if(city!=null){
			if(city.getLevel()==1){
				record.setProvince(city.getTitle());
			}
			if(city.getLevel()==2){
				record.setProvince(city.getParentName());
				record.setCity(city.getTitle());
			}
			if(city.getLevel()==3){
				record.setProvince(city.getRootName());
				record.setCity(city.getParentName());
				record.setCounty(city.getTitle());
			}
			record.setCountyId(cityparentid);
		}
		
		String bankBranchId=pd.getString("bankBranchId");
		HhBankBranch bankBranch=bankBranchMapper.selectByPrimaryKey(bankBranchId);
		if(bankBranch!=null){
			record.setBankBranch(bankBranch.getBranchName());
		}else{
			record.setBankBranchId(null);
		}
		if(pd.getLong("agentId")>0){
			 //编辑
			hhAgentMapper.updateByPrimaryKeySelective(record);
		}else{
			record.setAddTime(TimeManager.currentTime());
			hhAgentMapper.insertSelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteHhAgentById(long agentId) {
		return hhAgentMapper.deleteByPrimaryKey(agentId);
	}
	
	/**
	*列表分页
	*/
	public PageData selectHhAgentList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
		String advanceFilter=pd.getString("advanceFilter");
		if(advanceFilter!=null&&!advanceFilter.equals("")){
			advanceFilter=advanceFilter.replaceAll("'", "");
			JSONArray array=JSONObject.parseArray(advanceFilter);
			StringBuffer buffer=new StringBuffer();
			for(int i=0;i<array.size();i++){
				JSONObject object=array.getJSONObject(i);
				String field=object.getString("field");
				String op=object.getString("op");//equal  contains  notequal  greater  greaterorequal  less  lessorequal  beginwith  endwith
				String value=object.getString("value");
				buffer.append(" and ").append(TransFerUtil.getMybatisField(field)).append(TransFerUtil.getMybatisOperation(op,value));
			}
			if(array.size()>0){
				pd.put("condition", buffer.toString());
			}
		}
		List<HhAgent> records= hhAgentMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<HhAgent>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<HhAgent> listAll(PageData pd) {
		return  hhAgentMapper.selectAll(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public HhAgent selectByHhAgentId(Long agentId) {
		HhAgent agent=hhAgentMapper.selectByPrimaryKey(agentId);;
		String bankBranchId=agent.getBankBranchId();
		if(!StringUtils.isEmpty(bankBranchId)){
			HhBankBranch bankBranch=bankBranchMapper.selectByPrimaryKey(bankBranchId);
			agent.setBankProvince(bankBranch.getProvince());
			agent.setBankProvinceId(bankBranch.getProvinceId());
			agent.setBankCity(bankBranch.getCity());
			agent.setBankCityId(bankBranch.getCityId());
			agent.setBankId(bankBranch.getParentId());
		}
		return agent;
	}

	public List<HhAgent> selectHhAgentListTree(PageData pd) {
		return hhAgentMapper.selectListAllTree(pd);
	}

	public HhAgent insertOrSelectByPrimaryKey() {
		 HhAgent agent=hhAgentMapper.selectByPrimaryKey(1L);
		 if(agent==null){
			 agent=new HhAgent();
			 agent.setAgentId(1L);
			 agent.setName("系统平台");
			 agent.setBalance(new BigDecimal("0"));
			 hhAgentMapper.insertSelective(agent);
		 }
		return agent;
	}
}

